<template>
  <div class="container"
       v-if="type=='1'">
    <mt-search autofocus
               v-model="companyNameSearch"
               cancel-text="取消"
               placeholder="请输入公司名称"
               style="padding-bottom: 36px;"
               show>
      <div class="mint-search-list">
        <div class="mint-search-list-warp"
             style="overflow: scroll;margin-bottom:36px;">
          <a class="mint-cell"
             v-for="item in companyList"
             @click="selectCompany(item)">
            <div class="mint-cell-left"></div>
            <div class="mint-cell-wrapper">
              <div class="mint-cell-title"><span class="mint-cell-text">{{item.agentCompanyName}}</span></div>
            </div>
            <div class="mint-cell-right"></div>
          </a>
        </div>
      </div>
    </mt-search>

    <div class="operation-btn postion-fix"
         v-if="total>10">
      <a class="operation-btn-link"
         @click="companyLoadMore('per')">上一页</a>
      <a class="operation-btn-link"
         @click="companyLoadMore('next')">下一页</a>
    </div>
    <div class="result-box"
         v-if="type=='1'&&noResult">
      <div class="result-false"></div>
    </div>
  </div>

  <div class="container"
       v-else-if="type=='2'">
    <mt-search autofocus
               v-model="agent"
               cancel-text="取消"
               placeholder="请输入姓名/手机号码"
               style="padding-bottom: 36px"
               show>
      <div class="mint-search-list">
        <div class="mint-search-list-warp">
          <a class="mint-cell"
             v-for="item in agentList"
             @click="selectPartner(item)">
            <div class="mint-cell-left"></div>
            <div class="mint-cell-wrapper">
              <div class="mint-cell-title"
                   style="flex:2"> <span class="mint-cell-text">{{substring(companyName,9)}}</span></div>
              <div class="mint-cell-value"
                   style="flex:1"><span>{{substring(item.accountName,4)}}</span></div>
              <div class="mint-cell-value"
                   style="flex:1"><span>{{item.phone}}</span></div>
            </div>
            <div class="mint-cell-right"></div>
          </a>
        </div>
      </div>
    </mt-search>

    <div class="operation-btn postion-fix"
         v-if="this.total>10">
      <a class="operation-btn-link"
         @click="partnerLoadMore('per')">上一页</a>
      <a class="operation-btn-link"
         @click="partnerLoadMore('next')">下一页</a>
    </div>
    <div class="result-box"
         v-if="type=='2'&&noResult">
      <div class="result-false"></div>
    </div>
  </div>

</template>
<script>
import { Toast, MessageBox, Popup, Search } from "mint-ui";
export default {
  data() {
    return {
      companyList: [],
      companyNameSearch: "",
      pageNo: 1,
      pageSize: 10,
      type: 0,
      companyId: 0,
      agent: "",
      agentList: [],
      total: 0,
      companyName: "",
      premisesId: "",
    };
  },
  computed: {
    noResult: function () {
      if (this.total > 0) {
        return false;
      } else {
        return true;
      }
    },
  },
  watch: {
    //监听搜索框 实时加载查询结果
    companyNameSearch: function (newValue, oldValue) {
      this.pageNo = 1;
      this.companyList = [];
      let url = this.utilHelper.apiUrl + "/api/getAgentOrgByCompanyList";
      var params = {
        agentCompanyName: newValue,
        pageNo: 1,
        pageSize: 10,
      };
      if (this.premisesId) {
        params.premisesId = this.premisesId;
      } else {
        return false;
      }
      var body = {};
      body.params = params;
      this.$http.post(url, body).then((response) => {
        console.log(response);
        let body = response.body;
        if (body.result == 1) {
          this.companyList = body.resData.agentCompanyList;
        } else {
          Toast({
            message: "数据获取失败",
            position: "middle",
            duration: 2000,
          });
        }
      });
      var countBody = {};
      countBody.params = {
        agentCompanyName: this.companyNameSearch,
        premisesId: this.premisesId,
      };
      let urlCount = this.utilHelper.apiUrl + "/api/getAgentOrgCountByCompany";
      this.$http.post(urlCount, countBody).then((response) => {
        console.log(response);
        let body = response.body;
        if (body.result == 1) {
          this.total = body.resData.count;
        } else {
          Toast({
            message: "数据获取失败",
            position: "middle",
            duration: 2000,
          });
        }
      });
    },
    agent: function (newValue, oldValue) {
      this.pageNo = 1;
      let url = this.utilHelper.apiUrl + "/api/getPartnerByAgentOrg";
      var body = {};
      body.params = {
        premisesId: this.premisesId,
        searchValue: newValue,
        companyId: this.companyId,
        pageNo: 1,
        pageSize: 10,
      };
      this.$http.post(url, body).then((response) => {
        console.info(response);
        let body = response.body;
        if (body.result == 1) {
          this.agentList = body.resData.agentList;
        } else {
          Toast({
            message: "数据获取失败",
            position: "middle",
            duration: 2000,
          });
        }
      });
      var countBody = {};
      countBody.params = {
        searchValue: newValue,
        premisesId: this.premisesId,
        companyId: this.companyId,
      };
      let urlCount = this.utilHelper.apiUrl + "/api/getPartnerCountByAgentOrg";
      this.$http.post(urlCount, countBody).then((response) => {
        console.info(response);
        let body = response.body;
        if (body.result == 1) {
          this.total = body.resData.resDate;
        } else {
          Toast({
            message: "数据获取失败",
            position: "middle",
            duration: 2000,
          });
        }
      });
    },
  },
  methods: {
    //分页查询公司
    companyLoadMore: function (page) {
      if (page == "next") {
        if (this.pageNo > this.total / 10) {
          Toast("最后一页");
          return;
        }
        this.pageNo = this.pageNo + 1;
      }
      if (page == "per") {
        if (this.pageNo > 1) {
          this.pageNo = this.pageNo - 1;
        } else {
          Toast("第一页");
          return;
        }
      }

      let url = this.utilHelper.apiUrl + "/api/getAgentOrgByCompanyList";
      let params = {
        premisesId: this.premisesId,
        agentCompanyName: this.companyNameSearch,
        pageNo: this.pageNo,
        pageSize: this.pageSize,
      };
      var body = {};
      body.params = params;
      this.$http.post(url, body).then((response) => {
        let body = response.body;
        if (body.result == 1) {
          this.companyList = body.resData.agentCompanyList;
        } else {
          Toast({
            message: "数据获取失败",
            position: "middle",
            duration: 2000,
          });
        }
      });
    },
    companyCount: function () {
      var params = {
        agentCompanyName: this.companyNameSearch,
      };
      if (this.premisesId) {
        params.premisesId = this.premisesId;
      } else {
        return false;
      }
      var body = {};
      body.params = params;
      let urlCount = this.utilHelper.apiUrl + "/api/getAgentOrgCountByCompany";
      this.$http.post(urlCount, body).then((response) => {
        let body = response.body;
        if (body.result == 1) {
          this.total = body.resData.count;
        } else {
          Toast({
            message: "数据获取失败",
            position: "middle",
            duration: 2000,
          });
        }
      });
    },
    //分页查询经济人
    partnerLoadMore: function (page) {
      if (page == "next") {
        if (this.pageNo > this.total / 10) {
          Toast("最后一页");
          return;
        }
        this.pageNo = this.pageNo + 1;
      }
      if (page == "per") {
        if (this.pageNo > 1) {
          this.pageNo = this.pageNo - 1;
        } else {
          Toast("第一页");
          return;
        }
      }
      let url = this.utilHelper.apiUrl + "/api/getPartnerByAgentOrg";
      var body = {};
      body.params = {
        premisesId: this.premisesId,
        searchValue: this.agent,
        companyId: this.companyId,
        pageNo: this.pageNo,
        pageSize: this.pageSize,
      };
      this.$http.post(url, body).then((response) => {
        console.info(response);
        let body = response.body;
        if (body.result == 1) {
          this.agentList = body.resData.agentList;
          console.info(this.agentList);
        } else {
          Toast({
            message: "数据获取失败",
            position: "middle",
            duration: 2000,
          });
        }
      });
    },
    partnerCount: function () {
      var countBody = {};
      countBody.params = {
        searchValue: this.agent,
        premisesId: this.premisesId,
        companyId: this.companyId,
      };
      let urlCount = this.utilHelper.apiUrl + "/api/getPartnerCountByAgentOrg";
      this.$http.post(urlCount, countBody).then((response) => {
        let body = response.body;
        if (body.result == 1) {
          this.total = body.resData.resDate;
        } else {
          Toast({
            message: "数据获取失败",
            position: "middle",
            duration: 2000,
          });
        }
      });
    },
    //选择公司
    selectCompany: function (item) {
      this.$router.push({
        path: "/report",
        query: {
          companyName: item.agentCompanyName,
          companyId: item.agentCompany,
          type: 1,
        },
      });
    },
    //选择经纪人
    selectPartner: function (item) {
      this.$router.push({
        path: "/report",
        query: { accountName: item.accountName, agentId: item.id, type: 2 },
      });
    },
    substring: function (value, size) {
      if (value == null || typeof value == "undefined") {
        return null;
      }
      if (value.length > size) {
        return value.substring(0, size) + "...";
      } else {
        return value;
      }
    },
  },
  mounted() {
    //加载所有列表
    this.type = this.$route.query.type;
    this.companyId = this.$route.query.companyId;
    this.companyName = this.$route.query.companyName;
    this.type = this.$route.query.type;
    this.premisesId = this.$route.query.premiseId;
    if (this.type == "1") {
      this.companyCount();
      this.companyLoadMore();
    } else if (this.type == "2") {
      this.partnerCount();
      this.partnerLoadMore("");
    }
  },
};
</script>
<style>
.container {
  postion: relative;
}
.operation-btn {
  padding: 6px 10px;
  background-color: #fffef6;
  border-top: 1px solid #cdcdcd;
  bottom: 0;
  display: block;
  width: 100%;
}
.operation-btn .operation-btn-link {
  width: 50%;
  float: left;
  text-align: center;
  height: 24px;
  line-height: 24px;
  border-right: 1px solid #cdcdcd;
  color: #eb6100;
  font-size: 14px;
}
.operation-btn .operation-btn-link:last-child {
  border-right: none;
}
.operation-btn:after {
  content: "";
  display: block;
  clear: both;
  visibility: hidden;
  overflow: hidden;
  font-size: 0;
  height: 0;
}

.result-false {
  background: url("../assets/images/noresult.png") no-repeat scroll center
    center transparent;
  height: 100%;
}
.result-box {
  background-color: #fff;
  position: absolute;
  top: 0px;
  bottom: 0px;
  left: 0px;
  right: 0px;
}
.postion-fix {
  position: fixed;
}
</style>